﻿using OrderTrace;
using System;
using System.Collections.Generic;
using System.Text;
using Volo.Abp;
using Volo.Abp.DependencyInjection;
using WorkflowCore.Interface;
using WorkflowCore.Models;
using Yhd.Captcha.Core.OtpNet;

namespace Zt.QmtqDemo.ConfmPlan.Saga
{
    public class OrderTraceTask : StepBody, ITransientDependency
    {
        private IOrderTrace001AppService trace001AppService;

        public OrderTraceTask(IOrderTrace001AppService orderTrace001AppService)
        {
            this.trace001AppService = orderTrace001AppService;
        }

        public List<ConfmMatDto> MatDtos { get; set; }

        public string Message { get; set; }

        public int Code { get; set; }

        public override ExecutionResult Run(IStepExecutionContext context)
        {
            List<OrderTrace001Input> orderTrace001Inputs = new List<OrderTrace001Input>();
            foreach(var mat in MatDtos)
            {
                orderTrace001Inputs.Add(new OrderTrace001Input() { 
                     MatNo = mat.MatNo, OrderNo = mat.OrderNo , Wt = mat.MatWt, WholeBackCodeSeq = mat.NextWholeBacklogSeq, WholeBackLogNo = mat.WholeBacklogNo });
            }
            var orderTraceResult = this.trace001AppService.OrderTace001(orderTrace001Inputs).Result;

            if (orderTraceResult.Status)
            {
                return ExecutionResult.Next();
            }
            else
            {
                this.Message = orderTraceResult.Message;
                this.Code = -1;
                throw new BusinessException("500", this.Message);
            }
        }
    }
}
